package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/go-gin-admin/model"
	"github.com/go-gin-admin/util"
	"net/http"
)

// 根据角色获取绑定的菜单
func GetMenuByRoleId(c *gin.Context) {
	var roleMenu model.RoleMenu
	roleId, _ := util.StringToInt64(c.DefaultQuery("roleId", "0"))
	menus, err := roleMenu.GetMenuIds(roleId)
	if err != nil {
		c.JSON(http.StatusOK, new(util.Response).Err("查询失败"))
		return
	}
	m := make(map[string]interface{})
	m["item"] = menus
	c.JSON(http.StatusOK, new(util.Response).Ok("", m))
}

// 获取element-ui需要的菜单tree
func GetMenuElementUiTree(c *gin.Context) {
	var menuLabel model.MenuLabel
	list, err := menuLabel.GetMenuElementUiTree()
	if err != nil {
		c.JSON(http.StatusOK, new(util.Response).Err("查询失败"))
		return
	}
	m := make(map[string]interface{})
	m["tree"] = list
	c.JSON(http.StatusOK, new(util.Response).Ok("", m))
}

// 根据用户id获取所拥有的角色
func GetRoleIdsByUserId(c *gin.Context) {
	var roleMenu model.RoleMenu
	// 获取参数
	userId, _ := util.StringToInt64(c.DefaultQuery("userId", "0"))
	userIds, err := roleMenu.GetRoleIdsByUserId(userId)
	if err != nil {
		c.JSON(http.StatusOK, new(util.Response).Err("查询失败"))
		return
	}
	m := make(map[string]interface{})
	m["item"] = userIds
	c.JSON(http.StatusOK, new(util.Response).Ok("", m))
}
